using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

[CreateAssetMenu(fileName = "Effect16-目标棋子在几回合后才可以执行效果", menuName = "Skill/Effect/Effect16-目标棋子在几回合后才可以执行效果", order = 16)]
public class Effect16 : EffectBase
{
    public int turnCount;
    public EffectBase effect;
    private Dictionary<Chess,int> RecordingChessDic = new Dictionary<Chess,int>();
    public override void Apply(Pair pair, GameEvent e, List<Chess> targets)
    {
        if(targets == null) return;
        foreach (var chess in targets)
        {
            RecordingChessDic.TryAdd(chess,turnCount);
            RecordingChessDic[chess]--;
            if (RecordingChessDic[chess] != 0) continue;
            effect.Apply(pair, e, targets);
            RecordingChessDic.Remove(chess);
        }
    }
    
    public void ReviseDictionary(object info1 ,object info2)
    {
        var chess = info1 as Chess;
        if (chess == null) throw new ArgumentNullException(nameof(chess));
        var count = (int)info2;
        RecordingChessDic[chess] = count;
    }

    public override void ExternallyFunctions<T>(Chess info1,T info2)
    {
        ReviseDictionary(info1,info2);
    }
    

    public override void RemoveDictionary(Pair pair)
    {
        RecordingChessDic.Remove(pair.Chess);
    }

    private void OnDestroy()
    {
        RecordingChessDic.Clear();
    }
}